package com.group.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.entity.Talents;
import com.group.mapper.TalentsMapper;
import com.group.service.TalentsService;
import com.sun.org.apache.xpath.internal.operations.Mod;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 朋心合力
 * @since 2021-08-09
 */
@Controller
//@RequestMapping("/talents")
public class TalentsController {

    @Autowired
    TalentsService talentsService;

    @Autowired
    TalentsMapper talentsMapper;

    //此分页函数部分后面放到service层
    /**
     * 分页函数
     *
     * @param currentPage 当前页数
     * @param pageSize    每一页的数据条数
     * @param list        要进行分页的数据列表
     * @return 当前页要展示的数据
     */
    private Page<Talents> getPages(Integer currentPage, Integer pageSize, List list) {
        Page page = new Page();
        int size = list.size();

        if (pageSize > size) {
            pageSize = size;
        }

        // 求出最大页数，防止currentPage越界
        int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

        if (currentPage > maxPage) {
            currentPage = maxPage;
        }

        // 当前页第一条数据的下标
        int curIdx = currentPage > 1 ? (currentPage - 1) * pageSize : 0;

        List pageList = new ArrayList();

        // 将当前页的数据放进pageList
        for (int i = 0; i < pageSize && curIdx + i < size; i++) {
            pageList.add(list.get(curIdx + i));
        }

        page.setCurrent(currentPage).setSize(pageSize).setTotal(list.size()).setRecords(pageList);
        return page;
    }




    //分页查询接口

    /**
     *
     * @param talents  多选框的所选标签
     * @param model     视图
     * @param page 当前页数
     * @param size 每页大小（每页显示几条）
     * @return
     */
    @GetMapping("/lookingTalentsByTag/{page}/{size}")
    public String lookingTalentsByTag(@RequestParam("talents")
                                              String[] talents,
                                      Model model,
                                      @PathVariable("page") Integer page,
                                      @PathVariable("size") Integer size,
                                      HttpSession session
    ) {
        List<Talents> talentsList1 = null;
        Page<Talents> talentsPage = null;
        if (talents.length == 1 && (talents[0].equals("全部技术领域"))) {
            ;
            talentsPage = getPages(page, size, talentsMapper.selectList(null));
            talentsList1 = talentsPage.getRecords();
            model.addAttribute("allPages", talentsPage.getPages());
        }
        if (talents.length == 1 && !(talents[0].equals("全部技术领域"))) {
            talentsPage = getPages(page, size, talentsService.findTalents(talents[0], null));
            talentsList1 = talentsPage.getRecords();
            model.addAttribute("allPages", talentsPage.getPages());
//            talentsList1.forEach(System.out::println);
        }
        if (talents.length == 2) {
            talentsPage = getPages(page, size, talentsService.findTalents(talents[0], talents[1]));
            talentsList1 = talentsPage.getRecords();
            session.setAttribute("allPages", talentsPage.getPages());
        }
        session.setAttribute("talentsList", talentsList1);
        return "redirect:/talentslist";
    }


    //Postman 测试分页成功
    @GetMapping("/talentslist")
    public String talentsListRedirect(HttpSession session, Model model) {
        List<Talents> talentsList = (List<Talents>) session.getAttribute("talentsList");
        Integer allPages = (Integer) session.getAttribute("allPages");
        model.addAttribute("talentsList",talentsList);
        model.addAttribute("allPages", allPages);
        return "lookingtalents";
    }

}

